package io.github.kimmking.kkconfigdemo;

import io.github.kimmking.kkconfig.annotation.EnableKKConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@EnableKKConfig
@EnableConfigurationProperties({DemoConfigurationProperties.class})
public class KkconfigDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(KkconfigDemoApplication.class, args);
    }

    @Value("${kk.a}")
    private String a;

    @Value("${kk.b}")
    private String b;

    @Value("${kk.a}.${kk.b}")
    private String c;

    @Autowired
    DemoConfigurationProperties properties;

    @Bean
    ApplicationRunner runner() {
        return args -> {
            System.out.println("kk config demo start...");
            System.out.println("kk.a = " + a);
            System.out.println("kk.b = " + b);
            System.out.println("c = ${kk.a}.${kk.b} = " + c);
            System.out.println("properties.a = " + properties.getA());
            System.out.println("properties.b = " + properties.getB());
        };
    }


    @RequestMapping("/demo")
    public String demo() {
        System.out.println("kk config demo start...");
        System.out.println("kk.a = " + a);
        System.out.println("kk.b = " + b);
        System.out.println("c = ${kk.a}.${kk.b} = " + c);
        System.out.println("properties.a = " + properties.getA());
        System.out.println("properties.b = " + properties.getB());
        return String.format("{\"kk.a\" : \"%s\",\n" +
                "\"kk.b\" : \"%s\",\n" +
                "\"c\" : \"%s\",\n" +
                "\"properties.a\" : \"%s\",\n" +
                "\"properties.b\" : \"%s\"\n}",
                a, b, c, properties.getA(), properties.getB());
    }

}
